package com.bbjob.service.impl;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSSClient;
import com.bbjob.dto.Result;
import com.bbjob.mapper.DeliveryExamineeScoresDetailMapper;
import com.bbjob.mapper.DeliveryExamineeScoresMapper;
import com.bbjob.model.DeliveryExamineeScores;
import com.bbjob.model.DeliveryExamineeScoresDetail;
import com.bbjob.model.ExamineeScores;

@Component
public class AsyncTask {
	
	@Value("${oss.accessKeyId}")
	private String accessKeyId;
	@Value("${oss.accessKeySecret}")
	private String accessKeySecret;
	@Value("${oss.endpoint}")
	private String endpoint;
	@Value("${oss.bucket}")
	private String bucket;
	@Resource
	private DeliveryExamineeScoresMapper deliveryExamineeScoresMapper;
	@Resource
	private DeliveryExamineeScoresDetailMapper deliveryExamineeScoresDetailMapper;
	@Resource
	private ExamineeScoresService examineeScoresService;
	@Resource
	private ExamineeScoresDetailService examineeScoresDetailService;
	
	
	@Async("asyncExecutor")
	public void syncExamScores(Integer id) {
		
		// 同步scores
		DeliveryExamineeScores scoreParam = new DeliveryExamineeScores();
		scoreParam.setExamInfoId(id);
		scoreParam.setField4(0);
		List<DeliveryExamineeScores> scores = deliveryExamineeScoresMapper.selectByParams(scoreParam);
		ExamineeScores scoreEntity = null;
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		
		for (DeliveryExamineeScores score : scores) {
			scoreEntity = new ExamineeScores();
			scoreEntity.setExamInfoId(score.getExamInfoId());
			scoreEntity.setqId(score.getqId());
			scoreEntity.setsNo(score.getsNo());
			scoreEntity.setsScore(score.getsScore());
			
			File file = new File(score.getqAnswerPath());
			if (file.exists()) {
				ossClient.putObject(bucket, file.getName(), file);
				scoreEntity.setqAnswerPath(file.getName());
			}
			
			Result result = examineeScoresService.syncScores(scoreEntity);
			
			if (result.getCode() == 200) {
				score.setField4(1);
				deliveryExamineeScoresMapper.updateByPrimaryKey(score);
			}
		}
		
		// 同步scores_detail
		DeliveryExamineeScoresDetail scoreDetailParam = new DeliveryExamineeScoresDetail();
		scoreDetailParam.setExamInfoId(id);
		scoreDetailParam.setField4(0);
		List<DeliveryExamineeScoresDetail> details = deliveryExamineeScoresDetailMapper.selectByParams(scoreDetailParam);
		DeliveryExamineeScoresDetail detailEntity = null;
		for (DeliveryExamineeScoresDetail detail : details) {
			detailEntity = new DeliveryExamineeScoresDetail();
			detailEntity.setExamInfoId(detail.getExamInfoId());
			detailEntity.setqExamineeAnswer(detail.getqExamineeAnswer());
			detailEntity.setqId(detail.getqId());
			detailEntity.setqStem(detail.getqStem());
			detailEntity.setqTopicScore(detail.getqTopicScore());
			detailEntity.setqType(detail.getqType());
			detailEntity.setqTypeClassify(detail.getqTypeClassify());
			detailEntity.setQueId(detail.getQueId());
			detailEntity.setQueNo(detail.getQueNo());
			detailEntity.setsNo(detail.getsNo());
			
			Result result = examineeScoresDetailService.syncScoreDetail(detailEntity);
			
			if (result.getCode() == 200) {
				detail.setField4(1);
				deliveryExamineeScoresDetailMapper.updateByPrimaryKey(detail);
			}
		}
	}
}
